﻿using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PMAF.K3Cloud.SCM.BillList.BusinessPlugIn
{
    [HotUpdate]
    [Description("采购入库单根据流水号更新批号")]
    public class InStockList : AbstractListPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            switch (e.BarItemKey.ToUpperInvariant())
            {
                case "PMAF_UPDATELOTBTN":
                    //获取所有已选择行的数据
                    ListSelectedRowCollection selectedRows = ListView.SelectedRowsInfo;
                    //获取行选择信息
                    var listDatas = ListModel.GetData(selectedRows);
                    HashSet<int> ids = new HashSet<int>();

                    foreach ( var data in listDatas)
                    {
                        ids.Add(Convert.ToInt32(data["FID"]));
                    }
                    //设置采购订单过滤条件并查询订单
                    OQLFilter inStockFilter = new OQLFilter
                    {
                        new OQLFilterHeadEntityItem() { FilterString = string.Format("FID IN {0}", GetIds(ids)) }
                    };
                    DynamicObject[] inStocks = BusinessDataServiceHelper.Load(Context, "STK_INSTOCK", null, inStockFilter);
                    foreach(DynamicObject inStock in inStocks)
                    {
                        DynamicObjectCollection inStockEntries = (DynamicObjectCollection)inStock["InStockEntry"];
                        foreach(DynamicObject inStockEntry in inStockEntries)
                        {
                            DynamicObject purchaseSn = (DynamicObject)inStockEntry["F_PMAF_PurchaseSnId"];
                            string purchaseSnNo = purchaseSn["Number"].ToString();
                            inStockEntry["Lot_Text"] = purchaseSnNo;
                        }
                    }
                    IMetaDataService metaService = ServiceFactory.GetService<IMetaDataService>(Context);
                    FormMetadata inStockMeta = (FormMetadata)metaService.Load(Context, "STK_INSTOCK");
                    BusinessInfo businessInfo = inStockMeta.BusinessInfo;
                    DynamicObject[] dynamicObjects = BusinessDataServiceHelper.Save(Context, businessInfo, inStocks, "save");
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 把int数组转化为SQL语句的IN后面括号部分
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        private string GetIds(HashSet<int> ids)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("(");
            int i = 0;
            foreach (var id in ids)
            {
                sb.Append(id.ToString());
                if (i < ids.Count - 1)
                {
                    sb.Append(", ");
                }
                i++;
            }
            sb.Append(")");
            return sb.ToString();
        }
    }
}
